package com.hrm.controller;

import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hrm.dto.EmpAttendanceDTO;
import com.hrm.dto.EmpDepDTO;
import com.hrm.dto.EmpScalesRatingDTO;
import com.hrm.entity.Employee;
import com.hrm.entity.MyPage;
import com.hrm.service.AttendanceService;
import com.hrm.service.DepartmentService;
import com.hrm.service.EmployeeService;
import com.hrm.utils.JsonMsg;
import com.hrm.utils.Layui;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: zy
 * @Date: 2020/5/28 17:40
 * @Description: 出勤表获取,签到等
 */
@Controller
@RequestMapping("/attendance")
public class AttendanceController extends BaseController {
    @Autowired
    AttendanceService  attendanceService;
    @Autowired
    EmployeeService employeeService;

    /**
     * 跳转员工考勤页面
     * @return String
     */
    @RequestMapping("/toEmpAttendance.html")
    public String toEmpAttendance(){
        return "dailyManagement/empAttendance";
    }

    /**
     * 员工考勤打卡
     * @return JsonMsg
     */
    @RequestMapping("/empAttendance.html")
    @ResponseBody
    public JsonMsg empAttendance(){
        String status = attendanceService.empAttendance(session);
        if (status.equals("打卡成功,您已到勤") || status.equals("打卡成功,您已迟到")){
            return JsonMsg.success().addInfo("ATTENDANCE",status);
        }else {
            return JsonMsg.fail().addInfo("ATTENDANCE",status);
        }

    }

    /**
     * 部门人员统计
     * @param myPage
     * @return ResponseEntity<Layui>
     */
    @RequestMapping("/empStatistical.html")
    @ResponseBody
    public ResponseEntity<Layui> empStatistical(MyPage myPage){
        PageHelper.startPage(myPage.getPageNum(),myPage.getPageSize());
        Employee employee = (Employee) session.getAttribute("user");
        List<EmpDepDTO> empDepDTOList = employeeService.queryEmpDepByDepId(employee.getDeptId());
        List<EmpAttendanceDTO> empAttendanceDTOList = new ArrayList<EmpAttendanceDTO>();
        for(EmpDepDTO empDepDTO:empDepDTOList){
            EmpAttendanceDTO empAttendanceDTOInfo = attendanceService.empStatistical(empDepDTO.getId(),empDepDTO.getDeptId());
            if(empAttendanceDTOInfo!=null) {
                empAttendanceDTOList.add(empAttendanceDTOInfo);
            }
        }
        PageInfo pageInfo = new PageInfo(empAttendanceDTOList,5);
        Layui layui = Layui.data(pageInfo.getTotal(),pageInfo.getList());
        return ResponseEntity.ok(layui);
    }
}
